package com.cloud.poetry.core.entity;

import cn.hutool.core.util.StrUtil;
import com.cloud.poetry.core.constant.StateConstant;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Objects;

/**
 * 接口通用返回体
 * @author mengxj
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class R<T> {

    private Integer code;

    private T data;

    private String message;

    public R(int code, T t){
        this(code, t, "");
    }

    public static <T> R<T> success() {
        return success(null);
    }

    public static <T> R<T> success(T t) {
        return new R<>(StateConstant.SUCCESS, t);
    }

    public static <T> R<T> fail(T t){
        return new R<>(StateConstant.ERROR, null, Objects.isNull(t) ? StrUtil.EMPTY : t.toString());
    }
}
